package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ChatMapper;
import com.atguigu.spzx.manager.service.ChatService;
import com.atguigu.spzx.manager.service.HomeService;
import com.atguigu.spzx.model.dto.chat.MessageDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.chat.chatmessageVo;
import com.atguigu.spzx.model.vo.chat.contactsVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.spzx.model.vo.chat.contactsVo;
import com.atguigu.spzx.model.dto.chat.idInformationDto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    public Long GetUserSId(){
        SysUser sysUser = AuthContextUtil.get();
        Long userSId = sysUser.getId();
        return userSId;
    }


    @Override
    public List<contactsVo> GetAllContacts() {
        Long userSId = GetUserSId();
        List<contactsVo> list = chatMapper.GetAllContacts(userSId);
        return list;
    }

    @Override
    public void DeleteContacts(Long userRId) {
        Long userSId = GetUserSId();
        chatMapper.DeleteContacts(userSId,userRId);
    }

    @Override
    public void SaveMessage(MessageDto messageDto) {
        //获取发送者的id
        Long userSId = GetUserSId();
        messageDto.setUserSId(userSId);
        // 根据 senderId 和 receiverId 查找 conversationId
        Long conversationId = chatMapper.findConversationIdByUserIds(messageDto.getUserSId(), messageDto.getUserRId());
        // 如果 conversationId 不存在，则创建新的 conversation
        if (conversationId == null) {
            conversationId = chatMapper.createConversation(messageDto.getUserSId(), messageDto.getUserRId());
        }
        // 保存消息
        chatMapper.saveMessage(conversationId, messageDto.getUserSId(), messageDto.getContent());
    }

    @Override
    public List<Map<String, String>> GetOldMessage(idInformationDto idInformationDto) {
        Long userSId = idInformationDto.getUserSId();
        Long userRId = idInformationDto.getUserRId();
        //查询conversations表的对应id
        List Conversationslist = chatMapper.FindConversations(userSId,userRId);
        //System.out.println(Conversationslist);
        //根据查询conversation查出的id去查询message表的senderId和content
        List<chatmessageVo> list = chatMapper.FindMessages(Conversationslist);
        //System.out.println(list);

        // 创建一个列表来存储键值对
        List<Map<String, String>> resultList = new ArrayList<>();

        // 遍历消息列表，将每条消息转换为键值对形式
        for (chatmessageVo message : list) {
            Map<String, String> messageMap = new HashMap<>();
            messageMap.put("senderId", message.getSenderId().toString());
            messageMap.put("content", message.getContent());
            resultList.add(messageMap);
        }
        return resultList;
    }

}
